// pages/filing/invite/invite.js
var QR = require("../../../utils/qrCode");
import {
  getFamilyMemberApi,
  getPersonInfoApi,
  deleteFamilyMember,
  searchFamilyName,
  createFamily
} from "../../../services/filing/filing"
import {
  GETSTORAGE
} from "../../../services/storage/storage"
import Message from '../../../miniprogram_npm/tdesign-miniprogram/message/index'
Page({
  data: {
    familyMemberList: [],
    familyNames: [],
    width: 600,
    height: 600,
    userPersonId: '',
    btnFlag: false,
    userFamilyId: '',
    showWarnConfirm: false,
    // 删除弹出框
    deleteDialog: false,
    deleteDialogName: '',
    deleteFamilyMemberId: '',
    createDialog: false,
    inputValue: '',
    userName: '',
    familyLength: '',
    filingCompletedFlag: false
  },
  //获取用户个人信息
  getUserInfo() {
    const userId = GETSTORAGE('userId')
    const personId = GETSTORAGE('personId')
    const familyId = GETSTORAGE('familyId')
    getPersonInfoApi({
      userId: userId
    }).then(res => {
      console.log(res, "用户个人信息");
      const {
        currentFamilyMemberId,
        currentFamilyId,
        familyIds,
        nickName
      } = res.data.data
      const list = []
      familyIds.forEach(item => {
        searchFamilyName({
          familyId: item
        }).then(res => {
          const name = res.data.data.familyName
          getFamilyMemberApi({
            familyId: item,
            familyMemberId: ''
          }).then(res => {
            let obj = {}
            obj.list = res.data.data
            obj.familyName = name
            obj.familyId = item
            if (familyId == item) {
              list.unshift(obj)
            } else {
              list.push(obj)
            }
            this.setData({
              userFamilyMemberId: currentFamilyMemberId,
              userFamilyId: currentFamilyId,
              familyMemberList: list,
              userPersonId: personId,
              userName: nickName,
              familyLength: list.length
            })
          })
        })

      })
    })
  },
  getURL(obj) {
    var size = this.setCanvasSize();
    QR.qrApi.draw(`https://zg.particlesoft.info/frontend/inviteView?familyMemberId=${obj.familyMemberId}&familyId=${obj.familyId}`, "qrcode", size.w, size.h);
  },
  // 计算二维码宽高
  setCanvasSize: function () {
    var that = this;
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / res.windowWidth; //不同屏幕下canvas的适配比例；设计稿是750宽
      var width = that.data.width / scale;
      var height = width; //canvas画布为正方形
      size.w = width;
      size.h = height;
    } catch (e) {
      console.log("获取设备信息失败" + e);
    }
    return size;
  },
  handlePopup(e) {
    const {
      familymemberid,
      familyid
    } = e.currentTarget.dataset;
    console.log(familymemberid, "家人");
    // 生成二维码
    this.getURL({
      familyMemberId: familymemberid,
      familyId: familyid
    })
    this.setData({
      showWarnConfirm: true
    })
  },
  // 关闭弹出框
  closeDialog() {
    this.setData({
      showWarnConfirm: false
    });
  },
  goFamily(e) {
    console.log(e);
    const {
      familyid
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/filing/family/family?family=family&familyId=${familyid}`,
    })
  },
  return () {
    if (this.data.filingCompletedFlag) {
      wx.navigateBack({
        delta: 1,
      })
    } else {
      wx.switchTab({
        url: '/pages/my/my',
      })
    }
  },
  closeDeleteDialog() {
    this.setData({
      deleteDialog: false
    })
  },
  showDeleteDialog(e) {
    const familyMemberId = e.currentTarget.dataset.familymemberid
    const name = e.currentTarget.dataset.name
    this.setData({
      deleteDialogName: name,
      deleteDialog: true,
      deleteFamilyMemberId: familyMemberId
    })
  },
  // 删除弹出框显示
  deleteFamilyMember() {
    deleteFamilyMember({
      familyMemberId: this.data.deleteFamilyMemberId
    }).then(res => {
      if (res.data.code == 200) {
        this.getUserInfo()
      }
      if (res.data.code == 500) {
        console.log(1111);
        Message.error({
          context: this,
          offset: [20, 32],
          duration: 2000,
          content: res.data.msg,
        });
      }
      this.closeDeleteDialog()
    })
  },

  // 刷新
  refresh() {
    this.getUserInfo()
  },
  showCreateFamily() {
    const value = this.data.userName + '的一家' + (this.data.familyLength + 1)
    this.setData({
      createDialog: true,
      inputValue: value
    })
  },
  closeCreateDialog() {
    this.setData({
      createDialog: false
    })
  },
  createInput(e) {
    console.log(e);
    const {
      value
    } = e.detail
    this.setData({
      inputValue: value
    })

  },
  createFamily() {
    const userId = GETSTORAGE('userId')
    createFamily({
      userId: userId,
      familyName: this.data.inputValue
    }).then(res => {
      console.log(res, "-----------------");
      this.closeCreateDialog()
      this.refresh()
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.inviteJoin && options.inviteJoin == 'inviteJoin') {
      this.setData({
        btnFlag: true
      })
    } else {
      this.setData({
        btnFlag: false
      })
    }
    if (options.msg && options.msg == 'filingCompleted') {
      console.log(options, '1');
      this.setData({
        filingCompletedFlag: true
      })
    } else {
      console.log(options, '2');
      this.setData({
        filingCompletedFlag: false
      })
    }
    this.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})